package com.hjm.project.formalPos.posTransferRecord.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.framework.web.domain.AjaxResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.formalPos.posTransferRecord.mapper.PosTransferRecordMapper;
import com.hjm.project.formalPos.posTransferRecord.domain.PosTransferRecord;
import com.hjm.project.formalPos.posTransferRecord.service.IPosTransferRecordService;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 划拨记录Service业务层处理
 * 
 * @author LOVE
 * @date 2020-11-27
 */
@Service
public class PosTransferRecordServiceImpl implements IPosTransferRecordService 
{
    @Autowired
    private PosTransferRecordMapper posTransferRecordMapper;

    /**
     * 查询划拨记录
     * 
     * @param id 划拨记录ID
     * @return 划拨记录
     */
    @Override
    public PosTransferRecord selectPosTransferRecordById(Long id)
    {
        return posTransferRecordMapper.selectPosTransferRecordById(id);
    }

    /**
     * 查询划拨记录列表
     * 
     * @param posTransferRecord 划拨记录
     * @return 划拨记录
     */
    @Override
    public List<PosTransferRecord> selectPosTransferRecordList(PosTransferRecord posTransferRecord)
    {
        return posTransferRecordMapper.selectPosTransferRecordList(posTransferRecord);
    }

    /**
     * 新增划拨记录
     * 
     * @param posTransferRecord 划拨记录
     * @return 结果
     */
    @Override
    public int insertPosTransferRecord(PosTransferRecord posTransferRecord)
    {
        return posTransferRecordMapper.insertPosTransferRecord(posTransferRecord);
    }

    /**
     * 修改划拨记录
     * 
     * @param posTransferRecord 划拨记录
     * @return 结果
     */
    @Override
    public int updatePosTransferRecord(PosTransferRecord posTransferRecord)
    {
        return posTransferRecordMapper.updatePosTransferRecord(posTransferRecord);
    }

    /**
     * 删除划拨记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePosTransferRecordByIds(String ids)
    {
        return posTransferRecordMapper.deletePosTransferRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除划拨记录信息
     * 
     * @param id 划拨记录ID
     * @return 结果
     */
    @Override
    public int deletePosTransferRecordById(Long id)
    {
        return posTransferRecordMapper.deletePosTransferRecordById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult transfer(PosTransferRecord posTransferRecord,String currentMerno) throws Exception{
        if (StringUtils.isBlank(posTransferRecord.getPosNo())){
            return AjaxResult.error("终端号为空");
        }
        if (null==posTransferRecord.getCurrentAgent()){
            return AjaxResult.error("所选终端所属服务商为空");
        }
        if (StringUtils.isBlank(currentMerno)){
            return AjaxResult.error("所选终端所属商户为空");
        }
        if (null==posTransferRecord.getTargetAgent()){
            return AjaxResult.error("目标服务商为空");
        }
        //商户费率政策
        List<Map<String,Object>> merRateInfo=posTransferRecordMapper.getMerchantRateInfo(currentMerno);
        //处理商户费率政策
        Map<String,String> merRateMap=handlerRate(merRateInfo,1);
        //服务商费率政策
        List<Map<String,Object>> agentRateInfo=posTransferRecordMapper.getAgentRateInfo(posTransferRecord.getTargetAgent());
        //处理服务商费率政策
        Map<String,String> agentRateMap=handlerRate(agentRateInfo,2);
        BigDecimal multiple = new BigDecimal("10000");
        //费率政策是否倒挂验证
        for (String key : merRateMap.keySet()) {
            String merRate=merRateMap.get(key);
            String agentRate=agentRateMap.get(key);
            if (StringUtils.isBlank(merRate)){
                return AjaxResult.error("原商户费率信息错误");
            }
            if (StringUtils.isBlank(agentRate)){
                return AjaxResult.error("目标服务商费率信息错误");
            }

            BigDecimal b1=new BigDecimal(merRate).multiply(multiple);
            BigDecimal b2=new BigDecimal(agentRate).multiply(multiple);
            if (b1.compareTo(b2)<0){
                return AjaxResult.error("目标服务商费率高于终端绑定商户费率");
            }
        }
        int result=posTransferRecordMapper.insertPosTransferRecord(posTransferRecord);
        if (result>0){
            Map<String,Object> params=new HashMap<>(16);
            params.put("target_agent",posTransferRecord.getTargetAgent());
            params.put("current_merno",currentMerno);
            params.put("pos_no",posTransferRecord.getPosNo());
            //分别修改 商户 终端信息 终端绑定 所属服务商编号
            posTransferRecordMapper.updateMerchantInfoAgentNum(params);
            posTransferRecordMapper.updateFormalPosAgentNum(params);
            posTransferRecordMapper.updateBranchPosAgentNum(params);
            return AjaxResult.success();
        }else{
            return AjaxResult.error("添加划拨记录失败");
        }

    }

    private Map<String,String> handlerRate(List<Map<String,Object>> rateInfo,int type)throws Exception{
        Map<String,String> result=new HashMap<>(16);
        if (1==type){
            for (Map<String,Object> map:rateInfo){
                String settleType=String.valueOf(map.get("SETTLE_TYPE"));
                String cardType=String.valueOf(map.get("CARD_TYPE"));
                String rateType=String.valueOf(map.get("RATE_TYPE"));
                String arContent=String.valueOf(map.get("AR_CONTENT"));
                arContent=arContent.replace("P*","");
                result.put(settleType+"_"+cardType+"_"+rateType,arContent);
            }
        }else if(2==type){
            for (Map<String,Object> map:rateInfo){
                String settlementType=String.valueOf(map.get("SETTLEMENT_TYPE"));
                String cardType=String.valueOf(map.get("CARD_TYPE"));
                String distributionType=String.valueOf(map.get("DISTRIBUTION_TYPE"));
                String settlementPrice=String.valueOf(map.get("SETTLEMENT_PRICE"));
                settlementPrice=settlementPrice.replace("P*","");
                result.put(settlementType+"_"+cardType+"_"+distributionType,settlementPrice);
            }
        }

        return result;
    }

}
